import '../db/entity/AttributeBean.dart';
import '../db/entity/CustomerBillAttr.dart';

///将一组数据固定分组，每组n个元素
List<List<AttributeBean>> splitListBySize(
    List<AttributeBean> resList, int subListLength) {
  if (resList.isEmpty) {
    return [];
  }
  List<List<AttributeBean>> ret = [];
  int size = resList.length;
  if (size <= subListLength) {
    // 数据量不足 subListLength 指定的大小
    ret.add(resList);
  } else {
    int pre = size ~/ subListLength;
    int last = size % subListLength;
    // 前面pre个集合，每个大小都是 subListLength 个元素
    for (int i = 0; i < pre; i++) {
      List<AttributeBean> itemList = [];
      for (int j = 0; j < subListLength; j++) {
        itemList.add(resList[i * subListLength + j]);
      }
      ret.add(itemList);
    }
    // last的进行处理
    if (last > 0) {
      List<AttributeBean> itemList = [];
      for (int i = 0; i < last; i++) {
        itemList.add(resList[pre * subListLength + i]);
      }
      //凑满一行
      if (last < subListLength - 1) {
        var lastReal = subListLength - last;
        for (int i = 0; i < lastReal; i++) {
          itemList.add(AttributeBean(
              createTime: DateTime.now(), updateTime: DateTime.now()));
        }
      }
      ret.add(itemList);
    }
  }
  return ret;
}

///将一组数据固定分组，每组n个元素
List<List<CustomerBillAttr>> split2CustomerBillAttr(
    List<CustomerBillAttr> resList, int subListLength) {
  if (resList.isEmpty) {
    return [];
  }
  List<List<CustomerBillAttr>> ret = [];
  int size = resList.length;
  if (size <= subListLength) {
    // 数据量不足 subListLength 指定的大小
    ret.add(resList);
  } else {
    int pre = size ~/ subListLength;
    int last = size % subListLength;
    // 前面pre个集合，每个大小都是 subListLength 个元素
    for (int i = 0; i < pre; i++) {
      List<CustomerBillAttr> itemList = [];
      for (int j = 0; j < subListLength; j++) {
        itemList.add(resList[i * subListLength + j]);
      }
      ret.add(itemList);
    }
    // last的进行处理
    if (last > 0) {
      List<CustomerBillAttr> itemList = [];
      for (int i = 0; i < last; i++) {
        itemList.add(resList[pre * subListLength + i]);
      }
      //凑满一行
      if (last < subListLength - 1) {
        var lastReal = subListLength - last;
        for (int i = 0; i < lastReal; i++) {
          itemList.add(CustomerBillAttr(
            billId: 0,
              productId: 0,
              productCode: '',
              productName: '',
              attributeId: 0,
              attributeCode: '',
              attributeName: '',
              attributeValue: '',
              createTime: DateTime.now(),
              updateTime: DateTime.now()));
        }
      }
      ret.add(itemList);
    }
  }
  return ret;
}
